<?php
namespace app\index\controller;
use think\Db;
class Order{
	public function add(){
		$p = input('post.');
		$xzsz = input('post.xzsz/s');
		$xzszo = json_decode($xzsz);
		Db::startTrans();
		try{
		$r = Db::execute('insert into gerenwangzhan.order values (null,?,?,?)',[intval($p['status']),$p['uuid'],intval($p['userId'])]);
		if($r>0){
			$oId = Db::getLastInsId();
			foreach($xzszo as $xzszitem){
				$r=Db::execute('insert into orderitem values (null,?,?,?)',[$xzszitem->sId,$oId,$xzszitem->goodsNum]);
				$dr = Db::execute('delete from cart where id=?',[$xzszitem->cId]);
			}	
		}
			Db::commit();
			return json(['code'=>200,'msg'=>'操作成功']);
		}catch(\Exception $e){
			Db::rollback();
			return json(['code'=>500,'msg'=>'操作失败']);
		}
	}
	public function query(){
		// $res = Db::query('select oId,uuid from orderitem right join 
		// (select id as oId,uuid from order where userId=1 and status=0)
		// on order.oId=orderitem.orderId');
		$orderR = Db::query('select id as oId,uuid from gerenwangzhan.order where userId=1 and status=0');
		foreach($orderR as $key=>$orderitem){
			$itemR = Db::query('select img,goodsNum,sId,sName,gName,gId,price from goodsskuimg right join 
			(select goodsNum,sId,sName,name as gName,gId,price from goods right join 
			(select goodsNum,sId,name as sName,goodsId as gId,price from goodssku right join 
			(select goodsNum,goodsSkuId as sId from orderitem where orderId=? limit 1) o
			on goodssku.id = o.sId) go
			on goods.id = go.gId) ggo
			on goodsskuimg.goodsskuid = ggo.sId limit 1',[$orderitem['oId']]);
			$orderR[$key]['info'] = $itemR;
		}
		return json(['code'=>200,'data'=>$orderR]);
	}
}